﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using Agen.ServiceAgencyWPF;
using Agen.Models;
using System.ComponentModel;
using System.Diagnostics;

namespace Agen.Views
{
    public partial class AddCustomerToSession : Page
    {

        public AddcustomerToSessionVO vo;

        public AddCustomerToSession()
        {
            InitializeComponent();
            vo = new AddcustomerToSessionVO();
            DataContext = vo;
            ApplicationManager.client.GetAllSessionsByCustomerAndOfferCompleted += new EventHandler<GetAllSessionsByCustomerAndOfferCompletedEventArgs>(client_GetAllSessionsByCustomerAndOfferCompleted);
            ApplicationManager.client.GetSessionsByCustomerIDCompleted += new EventHandler<GetSessionsByCustomerIDCompletedEventArgs>(client_GetSessionsByCustomerIDCompleted);
            ApplicationManager.client.DeleteSessionToCustomerCompleted += new EventHandler<DeleteSessionToCustomerCompletedEventArgs>(client_DeleteSessionToCustomerCompleted);
            ApplicationManager.client.AddSessionToUserCompleted += new EventHandler<AddSessionToUserCompletedEventArgs>(client_AddSessionToUserCompleted);
        }

        void client_DeleteSessionToCustomerCompleted(object sender, DeleteSessionToCustomerCompletedEventArgs e)
        {
            if (e.Result != 0)
            {
                refreshList();
            }
        }

        void client_AddSessionToUserCompleted(object sender, AddSessionToUserCompletedEventArgs e)
        {

            if(e.Result){
                refreshList();
            }
        }

        void client_GetSessionsByCustomerIDCompleted(object sender, GetSessionsByCustomerIDCompletedEventArgs e)
        {
            this.vo.SessionCustomerList = e.Result;
        }

        void client_GetAllSessionsByCustomerAndOfferCompleted(object sender, GetAllSessionsByCustomerAndOfferCompletedEventArgs e)
        {
            this.vo.SessionList = e.Result;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
              ApplicationManager.client.GetSessionsByCustomerIDAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void category_cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedItem != null && DataContext != null)
            {
                this.vo.TypeList = ApplicationManager.itemsource_type[(int)(sender as ComboBox).SelectedValue];
                this.vo.SubTypeList = new Dictionary<int, string>();
                this.vo.IdCategory = (int)(sender as ComboBox).SelectedValue;
                this.vo.IdSubType = -1;
                this.vo.IdType = -1;

            }


        }

        private void type_cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedItem != null && DataContext != null)
            {
                ComboBox combo = sender as ComboBox;
                this.vo.SubTypeList = ApplicationManager.itemsource_subtype[(int)(sender as ComboBox).SelectedValue];
                this.vo.IdType = (int)(sender as ComboBox).SelectedValue;
                this.vo.IdSubType = -1;
            }
        }

        private void subtype_cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ComboBox).SelectedItem != null && DataContext != null)
            {
                ComboBox combo = sender as ComboBox;
                this.vo.IdSubType = (int)(sender as ComboBox).SelectedValue;
            }

        }

        private void serach_btn_Click(object sender, RoutedEventArgs e)
        {
            refreshCustomerSessionList();
        }

        private void deleteCustomer_btn_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.client.DeleteSessionToCustomerAsync(((sender as Button).Tag as SessionDC).Id, ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void addCustomer_btn_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.client.AddSessionToUserAsync(((sender as Button).Tag as SessionByCustomer_Result).IdSession, ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void refreshList() {
            ApplicationManager.client.GetSessionsByCustomerIDAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
            refreshCustomerSessionList();
        }



        private void refreshCustomerSessionList() {
            if (this.vo.IdType == 1 || this.vo.IdType == 3 || this.vo.IdType == 16)
            {
                Debug.WriteLine("DBG  : " + ApplicationManager.CURRENT_CUSTOMER.IdCustomer + ',' + this.vo.IdType);
                ApplicationManager.client.GetAllSessionsByCustomerAndOfferAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer, this.vo.IdType, this.vo.DateSearch);
            }
            else if (this.vo.IdCategory != -1 && this.vo.IdType != -1 && this.vo.IdSubType != -1)
            {
                ApplicationManager.client.GetAllSessionsByCustomerAndOfferAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer, this.vo.IdSubType, this.vo.DateSearch);
            }
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }


    }

    public class AddcustomerToSessionVO : INotifyPropertyChanged
    {

        public AddcustomerToSessionVO()
        {
            this.DateSearch = DateTime.Now;

            this.SessionList = new ObservableCollection<SessionByCustomer_Result>();
            this.SessionCustomerList = new ObservableCollection<SessionDC>();
            this.CategoryList = new Dictionary<int, string>();
            this.SubTypeList = new Dictionary<int, string>();
            this.TypeList = new Dictionary<int, string>();
            this.CategoryList = ApplicationManager.Category;
            this.IdCategory = -1;
            this.IdType = -1;
            this.IdSubType = -1;

        }

        private Dictionary<int, string> _categoryList;
        private Dictionary<int, string> _typeList;
        private Dictionary<int, string> _subTypeList;
        private ObservableCollection<SessionByCustomer_Result> _sessionList;
        private ObservableCollection<SessionDC> _sessionCustomerList;
        private int _idType;
        private int _idSubType;
        private int _idCategory;
        private DateTime _dateSearch;


        public int IdCategory
        {
            get
            {
                return this._idCategory;
            }
            set
            {
                this._idCategory = value;
                OnPropertyChanged("IdCategory");
            }
        }

        public DateTime DateSearch
        {
            get
            {
                return this._dateSearch;
            }
            set
            {
                this._dateSearch = value;
                OnPropertyChanged("SessionList");
            }
        }

        public int IdType
        {
            get
            {
                return this._idType;
            }
            set
            {
                this._idType = value;
                OnPropertyChanged("IdType");
            }
        }

        public int IdSubType
        {
            get
            {
                return this._idSubType;
            }
            set
            {
                this._idSubType = value;
                OnPropertyChanged("IdSubType");
            }
        }

        public ObservableCollection<SessionByCustomer_Result> SessionList
        {
            get
            {
                return this._sessionList;
            }
            set
            {
                this._sessionList = value;
                OnPropertyChanged("SessionList");
            }
        }

        public ObservableCollection<SessionDC> SessionCustomerList
        {
            get
            {
                return this._sessionCustomerList;
            }
            set
            {
                this._sessionCustomerList = value;
                OnPropertyChanged("SessionCustomerList");
            }
        }

        public Dictionary<int, string> CategoryList
        {
            get
            {
                return this._categoryList;
            }
            set
            {
                this._categoryList = value;
                OnPropertyChanged("CategoryList");
            }
        }

        public Dictionary<int, string> TypeList
        {
            get
            {
                return this._typeList;
            }
            set
            {
                this._typeList = value;
                OnPropertyChanged("TypeList");
            }
        }

        public Dictionary<int, string> SubTypeList
        {
            get
            {
                return this._subTypeList;
            }
            set
            {
                this._subTypeList = value;
                OnPropertyChanged("SubTypeList");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

    }

}
